package UserInput;

import idp.MainController;
import java.awt.event.*;
import Command.*;
import debuggen.Debug;

/**
 * KeyboardInput
 * @author Danny
 * Class that takes care of the keyboard input.
 * 
 * Keys:
 * A - Rudder left
 * D - Rudder right
 * W - Engine forward
 * S - Engine out
 * X - Engine backward
 * P - Sail
 * Escape - Emergency situation (release everything)
 * ctrl + alt - Emergency quit. Stop everything, incl microcontroller.
 */

public class KeyboardInput implements UserInput,
									  KeyListener {
	
	// Last entered key
	private int intPrevKey = -1;
	// Reference to main controller
	private MainController mController;
	

	
	/**
	 * Constructor
	 */
	public KeyboardInput(MainController p_mcController)
	{
		// Get instance of main controller
		mController = p_mcController;
		
	}
	/**
	 * keyPressed
	 * Event is fired when a key is pressed on the keyboard.
	 * @param e
	 */
	@Override
	public void keyPressed(KeyEvent e) {
		// Check which key code the event contains.
		Command tmp;
		switch(e.getKeyCode()){
			//Rudder full left
			case KeyEvent.VK_LEFT:
				tmp = new Rudder();
				tmp.setValue(255);
				mController.processUserInput(tmp);
				break;
			//Rudder neutral
			case KeyEvent.VK_DOWN:
				tmp = new Rudder();
				tmp.setValue(128);
				mController.processUserInput(tmp);
				break;
			//Rudder full right
			case KeyEvent.VK_RIGHT:
				tmp = new Rudder();
				tmp.setValue(0);
				mController.processUserInput(tmp);
				break;
			// Engine forward
			case KeyEvent.VK_W:
				mController.processUserInput(new EngineForward());
				break;
			// Engine backward
			case KeyEvent.VK_X:
				mController.processUserInput(new EngineBackward());
				break;
			// Engine out
			case KeyEvent.VK_S:
				mController.processUserInput(new EngineOut());
				break;
			// Rudder left;
			case KeyEvent.VK_A:
				tmp = new Rudder();
				tmp.setValue(mController.getDataWrapper().getRudderPosition() + 5);
				mController.processUserInput(tmp);
				break;
			// Rudder right
			case KeyEvent.VK_D:
				tmp = new Rudder();
				tmp.setValue(mController.getDataWrapper().getRudderPosition() - 5);
				mController.processUserInput(tmp);
				break;
			case KeyEvent.VK_O:
				tmp = new Sail();
				tmp.setValue(mController.getDataWrapper().getSailPosition() + 32);
				mController.processUserInput(tmp);
				break;
			//  Sail right
			case KeyEvent.VK_P:
				tmp = new Sail();
				tmp.setValue(mController.getDataWrapper().getSailPosition() - 32);
				mController.processUserInput(tmp);
				break;
			// Keel up
			case KeyEvent.VK_U:
				mController.processUserInput(new KeelUp());
				break;
			// Keel down
			case KeyEvent.VK_J:
				mController.processUserInput(new KeelDown());
				break;
			// Emergency situation
			case KeyEvent.VK_ESCAPE:
				mController.processUserInput(new EmergencySituation());
				break;
			// Emergency stop 
			case KeyEvent.VK_CONTROL:
				// Skip, value will be placed in var.
				break;
			// Emergency stop
			case KeyEvent.VK_ALT:
				if(intPrevKey == KeyEvent.VK_CONTROL){
					// Emergency stop!
					mController.processUserInput(new EmergencyStop());
				}
				break;
			/**
			 * Default / unknown
			 * Unknown key is pressed.
			 */
			case KeyEvent.CHAR_UNDEFINED:
			default:
				if(Debug.DEBUGGEN){
					Debug.writeMessage("Keyboard UNKNOWN key: " + e.getKeyChar());
				}
		}
		// Put key in var for next round!
		intPrevKey = e.getKeyCode();
	}
	/**
	 * keyReleased
	 * @param e
	 */
	@Override
	public void keyReleased(KeyEvent e) {
		/**
		 * Ignored
		 */
	}
	/**
	 * keyTyped
	 * @param e
	 */
	@Override
	public void keyTyped(KeyEvent e) {
		/**
		 * Ignored
		 */	
	}

}
